const {
  WorkOrderMyIndexApi
} = require('../../../../common/apis/workOrder')
const {
  trim
} = require('../../../../common/mixins/formItemType')
const mixin_formItemType = require('../../../../common/mixins/formItemType')
const {
  WxValidate
} = require('../../../../utils/WxValidate')

const assistObj = {
  //  派单对象类型
  sendObjType: '',
  // 派单对象标识
  sendObjId: '',
  // 派单所属小组
  sendTeam: '',
  // 协作说明
  content: '',
  // 协作附件
  assistEnclosure: []
}

Component({
  behaviors: [mixin_formItemType],

  /**
   * 组件的属性列表
   */
  properties: {
    // 工单号
    orderNum: {
      type: String,
      value: ''
    }

  },

  /**
   * 组件的初始数据
   */
  data: {
    /**
     * 公共附件
     */
    assistEnclosure: [],
    /**
     * 协作集合
     */
    assistList: [assistObj],
    // 协作说明
    assistRemark: '',
    rules: {
      sendObjType: {
        required: true,
        // sendObjType: true,
      },
      content: {
        required: false,
        content: true
      }
    },
    message: {
      sendObjType: {
        required: '请选择协作人'
      },
      content: {
        required: '请输入协作事务'

      }


    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 添加协作人
    onAdd() {
      const {
        assistList
      } = this.data
      assistList.push(assistObj)

      this.setData({
        assistList: assistList
      })
    },
    onChangeTextArea(e) {
      const index = e.currentTarget.dataset.index;
      const {
        value
      } = e.detail

      this.setData({
        [`assistList[${index}].content`]: value
      });
      console.log(this.data);
    },
    // 公共附件
    onPublicUploadData(e) {
      const {
        value,
        type
      } = e.detail
      if (type === 'fileList') {
        this.setData({
          assistEnclosure: value
        })
      } else {
        let {
          assistEnclosure
        } = this.data
        assistEnclosure.push(...value)
        this.setData({
          assistEnclosure: assistEnclosure
        })
      }
    },
    //  监听附件的值
    onUploadData(e) {
      console.log('onUploadData');
      const {
        value,
        type
      } = e.detail
      const {
        index
      } = e.currentTarget.dataset;
      if (type === 'fileList') {
        this.setData({
          [`assistList[${index}].assistEnclosure`]: value

        })
      } else {
        let assistEnclosure = this.data.assistList[index].assistEnclosure
        assistEnclosure.push(...value)
        this.setData({
          [`assistList[${index}].assistEnclosure`]: assistEnclosure
        })
      }
      console.log(this.data.assistList);
    },
    onSelectDept(e) {
      const type = {
        "team": 2,
        "user": 1
      }

      const index = e.currentTarget.dataset.index;
      const typeValue = e.detail.value.key;
      const value = e.detail.value.value;
      const teamId = e.detail.value.teamId;

      this.setData({
        [`assistList[${index}]`]: {
          sendObjType: type[typeValue],
          sendObjId: value,
          sendTeam: type[typeValue] === 1 ? teamId : ''
        }
      });
    },
    submit(callback) {
      console.log(this.data, 'submit');
      const {
        orderNum,
        assistRemark,
        assistEnclosure,
        assistList
      } = this.data
      const params = {
        orderNum,
        assistRemark,
        assistEnclosure,
        assistList
      }
      const validate = new WxValidate(this.data.rules, this.data.message)
      // 自定义验证规则
      validate.addMethod('content', (value, param) => {
        if (value.trim()) {
          return true
        }
        return false
      }, '请输入协作事务')
      for (let i of params.assistList) {
        console.log(i, 'i=------');
        if (!validate.checkForm(i)) {
          const error = validate.errorList[0]
          wx.showToast({
            title: error.msg,
            icon: 'error'
          })
          return false
        }
      }
      WorkOrderMyIndexApi.collaborativeWorkOrder(params, ({
        code,
        msg
      }) => {
        if (code === 200) {
          wx.showToast({
            title: '处理成功',
            icon: "success"
          })
          wx.$_.delay(callback, 1000)

        } else {
          console.error(msg);
          wx.showToast({
            title: msg,
            icon: 'error'
          })
        }
      })

    },
  }
})